﻿Imports ADOConnection

Public Class frmLogin
    Dim cauKetNoi As String = ""
    Dim frmCall As frmMain

    Private Sub btDangNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDangNhap.Click
        Dim objConnection As IADOConnection
        Dim strConnect As String = XL_DULIEU.GetConnectStrFromFile("DBConnect.udl")
        strLoginName_SA = txtTenDangNhap.Text
        strPassword_SA = txtPassword.Text

        'If strLoginName_SA = "sa" Then
        '    Dim str As String = "Quyền '" + strLoginName_SA + "' không kết nối được."
        '    MessageBox.Show(str)
        '    Exit Sub
        'ElseIf strLoginName_SA = "hredu" Then
        '    Dim str As String = "Quyền '" + strLoginName_SA + "' không kết nối được."
        '    MessageBox.Show(str)
        '    Exit Sub
        'End If

        Dim g_objConnFactory As ADOConnection.IConnFactory = New CConnFactory(strConnect, strLoginName_SA, strPassword_SA)
        objConnection = g_objConnFactory.GetConnection
        If IsNothing(objConnection) Then
            Dim str As String = "Quyền '" + strLoginName_SA + "' không kết nối được."
            MessageBox.Show(str)
            Exit Sub
        Else
            XL_DULIEU.GanCauLenhKetNoi(strLoginName_SA, strPassword_SA)

            'kiem tra xem co phai la admin
            Dim IsAdmin As Boolean = False

            sUserName = txtTenDangNhap.Text
            'Dim sCauLenh As String = "Select IS_SRVROLEMEMBER ('sysadmin','" & sUserName & "')"
            Dim sCauLenh As String = "select IS_MEMBER ('db_owner')"
            IsAdmin = XL_DULIEU.LayGiaTri(sCauLenh)
            'neu la admin
            If IsAdmin = True Then
                frmCall.HienThi()
                Me.Close()
            Else
                MessageBox.Show("Bạn không có quyền phân quyền người dùng!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub btThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btThoat.Click
        frmCall.Close()
        Me.Close()
    End Sub
    Private Sub XuLyLoi()
        If (MessageBox.Show("Đăng nhập không thành công. Có muốn đăng nhập lại?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.No) Then
            Me.Close()
        Else
            txtTenDangNhap.Text = ""
            txtPassword.Text = ""
        End If
    End Sub

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        XL_DULIEU.LayConnectionString("DBConnect.udl")
        lblCSDL.Text = XL_DULIEU.GetGiaTriThanhPhan("Initial Catalog")
        Me.KeyPreview = True
    End Sub

    Public Sub New(ByVal f As frmMain)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        frmCall = f
    End Sub

    Private Sub frmLogin_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
        'nhan ESC la thoat
        If (e.KeyCode = Keys.Escape) Then
            frmCall.Close()
            Me.Close()
        End If
    End Sub
End Class